;!function($){
    //*****局部插件部分*****/
    $.fn.extend({
        /**
         * 分页插件
         * @param
         * @param
         * @param
         */
        pagination: function (settings) {
            var defaultSettings = {
                currentPage: 1,//当前页
                total: 100,//总计录数
                perPage: 10,//每页X条
                type: 'white',//分页控件默认样式，gray可选
                width: 32,//页面宽
                maxPages: 4,//最多显示页码数量
                dataText: true,//是否显示统计数据
                startEnd: false,//是否显示第一页最后一页
                prevNext: true,//是否显示上一页下一页
                pagingEvent: null,//异步刷新表格
                align: "center"
            };
            var settings = $.extend({}, defaultSettings, $(this).data("settings"), settings);
            $(this).data("settings", settings);
            if (!$(this).is(':empty')) {
                $(this).empty();
            }
            var total_pages = Math.floor((settings.total - 1) / settings.perPage) + 1;
            var $e = $();

            //生成页码
            var pages_num = settings.maxPages > total_pages ? total_pages : settings.maxPages,
                $pages_warpper = $("<div />").css({"width": settings.width * pages_num}),
                $lis = $(), _$li = $(), _$current = $();
            for (var i = 1; i <= total_pages; i++) {
                _$li = $("<li />").text(i).click(function () {
                    if ($(this).hasClass("current")) {
                        return false;
                    }
                    $(this).addClass("current").siblings(".current").removeClass("current");
                    set_position(this);
                    typeof settings.pagingEvent === 'function' && settings.pagingEvent.call(this,$(this).text());
                });
                if (i === parseInt(settings.currentPage)) {
                    _$current = _$li.addClass("current");
                }
                $lis = $lis.add(_$li);
            }
            $e = $pages_warpper.append($e.add($("<ul />").css({"width": settings.width * total_pages}).append($lis)));
            //生成上一页下一页功能按钮
            if (settings.prevNext) {
                $e = $("<span>上一页</span>").addClass("go_prev").click(function () {
                    $e.find(".current").prev().click();
                })
                .add($e)
                .add($("<span>下一页</span>").addClass("go_next").click(function () {
                    $e.find(".current").next().click();
                }));
            }

            //生成首页尾页功能按钮
            if (settings.startEnd) {
                $e = $("<span />").addClass("go_first").click(function () {
                    $e.find("li").eq(0).click();
                })
                    .add($e)
                    .add($("<span />").addClass("go_last").click(function () {
                        $e.find("li").eq(-1).click();
                    }));
            }
            //生成统计数据
            if (settings.dataText) {
                var str = "共<em class=\"total_pages\">" + total_pages + "</em>页<em>";
                $e = $("<label />").html(str).add($e);
            }

            //注入当前元素
            $(this).removeAttr("style").addClass("ui-pagination " + settings.type).append($e);
            $(this).find(".currentPage").width($(this).find(".total_pages").width());
            $(this).css({
                width: function () {
                    return $(this).outerWidth() + 1
                }
            })
                .find("li").eq(settings.currentPage - 1).click();
            set_position(_$current, 1);
            if (settings.align == "left") {
                $(this).css({float: "left"});
            } else if (settings.align == "center") {
                $(this).css({float: "none"});
            } else {
                $(this).css({float: "right"});
            }

            function set_position(elem, duration) {
                var pos = parseInt($(elem).text()) || 0,
                    middle = Math.round((pages_num + 1) / 2),
                    ml = 0;

                if (middle >= pos) {
                    ml = 0;
                } else if (pos - middle > total_pages - pages_num) {
                    ml = (pages_num - total_pages) * settings.width;
                } else {
                    ml = (middle - pos) * settings.width;
                }
                $e.find(".currentPage").text(pos).end()
                    .find("ul").stop(true, true).animate({"margin-left": ml}, duration || 600);
            }
            return $e;
        }


    })

}(jQuery)

